#include <Kernel.h>
#include <BootConsole.h>
#include <Hal/Manager.h>
#include <Multiboot2.h>

#include "Utils/Bitmap.h"
#include "Hal/PhysicalMemoryManager.h"

bool Kernel::Booting = true;

Kernel::Kernel()
{
}

void Kernel::Init(uint64_t p1, uint64_t p2)
{
	BootConsole::Init();
	
	if (!Hal::Manager::ValidateParameters(p1, p2))
		Bsod("The Boot Parameters are invalid");
	
	Environment::TotalMemory(Hal::Manager::GetMemoryKbs() * 1024);
	
	/* The following code is just to show that the Kernel hasn't halted... */
	BootConsole::Puts("RAM: ");
	if (Environment::TotalMemory() < 1024 * 1024 * 1024)
	{
		BootConsole::Putn(Environment::TotalMemory() / (1024 * 1024));
		BootConsole::Puts(" Mb\n");
	}
	else
	{
		BootConsole::Putn(Environment::TotalMemory() / (1024 * 1024 * 1024));
		BootConsole::Puts(" Gb\n");
	}

	/* Initialize the Physical Memory Manager */
	Hal::PhysicalMemoryManager::Init(Hal::Manager::GetFirstAvailablePage());
}

void Kernel::BootDone()
{
	Booting = false;
}

bool Kernel::IsBooting()
{
	return Booting;
}
void Kernel::Bsod(const char *message)
{
	if (!IsBooting())
	{
#if 0
		DisableInterrupts();
		HaltAPs();
#endif
	}
	
#if 0
	if (TextMode())
	{
#endif
		BootConsole::Clear(0x1F);
		BootConsole::Puts("A problem has been detected and Lymph has been shut down to prevent damage to your computer.\n\nError: ");
		BootConsole::Puts(message);
		BootConsole::Puts("\n\nIf this is the first time you've seen this Stop error screen, restart your computer.\n");
		BootConsole::Puts("If this screen appears again, please contact the developers at http://code.google.com/p/lymph\n");
		BootConsole::Puts("\nPlease press the Power button for 4-5 seconds and reboot.");
		// CTRL + ALT + CANC?
#if 0
	}
#endif

	Hal::Manager::Halt();
}